import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/dashboard',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '系统首页',
                    noAuth: true,
                },
                component: () => import('../views/dashboard.vue'),
            },

            /**
             * 财务管理
             */
            {
                path: '/bank-management',
                name: 'bank-management',
                meta: {
                    title: '银行管理',
                    permiss: '1',
                },
                component: () => import('../views/finance/bank.vue'),
            },

            {
                path: '/loan-management',
                name: 'loan-management',
                meta: {
                    title: '贷款管理',
                    permiss: '2',
                },
                component: () => import('../views/finance/loan.vue'),
            },

            {
                path: '/finance-management',
                name: 'finance-management',
                meta: {
                    title: '资金管理',
                    permiss: '2',
                },
                component: () => import('../views/finance/finance.vue'),
            },

            /**
             * 公司管理
             */


            {
                path: '/company-management',
                name: 'company-management',
                meta: {
                    title: '公司管理',
                    permiss: '12',
                },
                component: () => import('../views/company/company.vue'),
            },


            /**
             * 经营项目
             */

            {
                path: '/business-project-management',
                name: 'business-project-management',

                meta: {
                    title: '项目信息维护',
                    permiss: '3',
                },
                component: () => import('../views/business/project.vue'),

            },
            {
                path: '/business-utility-management',
                name: 'business-utility-management',

                meta: {
                    title: '水电费管理',
                    permiss: '3',
                },
                component: () => import('../views/business/utility.vue'),

            },
            {
                path: '/business-revenue-management',
                name: 'business-revenue-management',

                meta: {
                    title: '当日营收管理',
                    permiss: '3',
                },
                component: () => import('../views/business/revenue.vue'),

            },



            {
                path: '/building',
                name: 'building',
                meta: {
                    title: '楼栋信息管理',
                    permiss: '4',
                },
                component: () => import(/* webpackChunkName: "building" */ '../views/building/building.vue'),
            },
            {
                path: '/building-rental',
                name: 'building-rental',
                meta: {
                    title: '出租信息管理',
                    permiss: '4',
                },
                component: () => import(/* webpackChunkName: "building-rental" */ '../views/building/rental.vue'),
            },

            {
                path: '/property-project',
                name: 'property-project',
                meta: {
                    title: '项目信息维护',
                    permiss: '5',
                },
                component: () => import('../views/property/project.vue'),
            },
            {
                path: '/property-finance',
                name: 'property-finance',
                meta: {
                    title: '月度收支情况',
                    permiss: '5',
                },
                component: () => import('../views/property/finance.vue'),
            },

            {
                path: '/engineer-project',
                name: 'engineer-project',
                meta: {
                    title: '项目信息维护',
                    permiss: '6',
                },
                component: () => import('../views/engineer/project.vue'),

            },

            {
                path: '/elder-management',
                name: 'elder-management',
                meta: {
                    title: '老人管理',
                    permiss: '7',
                },
                component: () => import('../views/elder/info.vue'),
            },

            {
                path: '/elder-finance',
                name: 'elder-finance',
                meta: {
                    title: '老人管理',
                    permiss: '7',
                },
                component: () => import('../views/elder/finance.vue'),

            },

            {
                path: '/weijian-project',
                name: 'weijian-project',
                meta: {
                    title: '项目信息维护',
                    permiss: '8',
                },
                component: () => import('../views/weijian/project.vue'),
            },
            {
                path: '/weijian-worker',
                name: 'weijian-worker',
                meta: {
                    title: '派工管理',
                    permiss: '8',
                },
                component: () => import('../views/weijian/worker.vue'),
            },
            {
                path: '/area-management',
                name: 'area-management',
                meta: {
                    title: '区域管理',
                    permiss: '9',
                },
                component: () => import( '../views/maintenance/area.vue'),
            },
            {
                path: '/area-work-type',
                name: 'area-work-type',
                meta: {
                    title: '作业种类管理',
                    permiss: '9',
                },
                component: () => import('../views/maintenance/worktype.vue'),
            },

            {
                path: '/area-work-log',
                name: 'area-work-log',
                meta: {
                    title: '每日作业情况',
                    permiss: '9',
                },
                component: () => import('../views/maintenance/worklog.vue'),
            },


            {
                path: '/car-info',
                name: 'car-info',
                meta: {
                    title: '车辆信息维护',
                    permiss: '10',
                },
                component: () => import('../views/car/carinfo.vue'),
            },

            {
                path: '/insurance-info',
                name: 'insurance-info',
                meta: {
                    title: '保险情况',
                    permiss: '10',
                },
                component: () => import('../views/car/insurance.vue'),
            },
            {
                path: '/mileage-info',
                name: 'mileage-info',
                meta: {
                    title: '每日里程更新',
                    permiss: '10',
                },
                component: () => import('../views/car/dailymileage.vue'),
            },
            {
                path: '/oil-info',
                name: 'oil-info',
                meta: {
                    title: '加油情况',
                    permiss: '10',
                },
                component: () => import('../views/car/refuelrecord.vue'),
            },

            {
                path: '/employee',
                name: 'employee',
                meta: {
                    title: '员工信息维护',
                    permiss: '12',
                },
                component: () => import('../views/employee/employee.vue'),
            },
            {
                path: '/user-management',
                name: 'user-management',
                meta: {
                    title: '用户管理',
                    permiss: '13',
                },
                component: () => import('../views/user/user.vue'),
            },



            {
                path: '/system-role',
                name: 'system-role',
                meta: {
                    title: '角色管理',
                    permiss: '12',
                },
                component: () => import(/* webpackChunkName: "system-role" */ '../views/system/role.vue'),
            },
            {
                path: '/system-menu',
                name: 'system-menu',
                meta: {
                    title: '菜单管理',
                    permiss: '13',
                },
                component: () => import(/* webpackChunkName: "system-menu" */ '../views/system/menu.vue'),
            },
            {
                path: '/table',
                name: 'basetable',
                meta: {
                    title: '基础表格',
                    permiss: '31',
                },
                component: () => import(/* webpackChunkName: "table" */ '../views/table/basetable.vue'),
            },
            {
                path: '/table-editor',
                name: 'table-editor',
                meta: {
                    title: '可编辑表格',
                    permiss: '32',
                },
                component: () => import(/* webpackChunkName: "table-editor" */ '../views/table/table-editor.vue'),
            },
            {
                path: '/schart',
                name: 'schart',
                meta: {
                    title: 'schart图表',
                    permiss: '41',
                },
                component: () => import(/* webpackChunkName: "schart" */ '../views/chart/schart.vue'),
            },
            {
                path: '/echarts',
                name: 'echarts',
                meta: {
                    title: 'echarts图表',
                    permiss: '42',
                },
                component: () => import(/* webpackChunkName: "echarts" */ '../views/chart/echarts.vue'),
            },

            {
                path: '/icon',
                name: 'icon',
                meta: {
                    title: '图标',
                    permiss: '5',
                },
                component: () => import(/* webpackChunkName: "icon" */ '../views/pages/icon.vue'),
            },
            {
                path: '/ucenter',
                name: 'ucenter',
                meta: {
                    title: '个人中心',
                },
                component: () => import(/* webpackChunkName: "ucenter" */ '../views/pages/ucenter.vue'),
            },
            {
                path: '/editor',
                name: 'editor',
                meta: {
                    title: '富文本编辑器',
                    permiss: '291',
                },
                component: () => import(/* webpackChunkName: "editor" */ '../views/pages/editor.vue'),
            },
            {
                path: '/markdown',
                name: 'markdown',
                meta: {
                    title: 'markdown编辑器',
                    permiss: '292',
                },
                component: () => import(/* webpackChunkName: "markdown" */ '../views/pages/markdown.vue'),
            },
            {
                path: '/export',
                name: 'export',
                meta: {
                    title: '导出Excel',
                    permiss: '34',
                },
                component: () => import(/* webpackChunkName: "export" */ '../views/table/export.vue'),
            },
            {
                path: '/import',
                name: 'import',
                meta: {
                    title: '导入Excel',
                    permiss: '33',
                },
                component: () => import(/* webpackChunkName: "import" */ '../views/table/import.vue'),
            },
            {
                path: '/theme',
                name: 'theme',
                meta: {
                    title: '主题设置',
                    permiss: '7',
                },
                component: () => import(/* webpackChunkName: "theme" */ '../views/pages/theme.vue'),
            },
            {
                path: '/calendar',
                name: 'calendar',
                meta: {
                    title: '日历',
                    permiss: '24',
                },
                component: () => import(/* webpackChunkName: "calendar" */ '../views/element/calendar.vue'),
            },
            {
                path: '/watermark',
                name: 'watermark',
                meta: {
                    title: '水印',
                    permiss: '25',
                },
                component: () => import(/* webpackChunkName: "watermark" */ '../views/element/watermark.vue'),
            },
            {
                path: '/carousel',
                name: 'carousel',
                meta: {
                    title: '走马灯',
                    permiss: '23',
                },
                component: () => import(/* webpackChunkName: "carousel" */ '../views/element/carousel.vue'),
            },
            {
                path: '/tour',
                name: 'tour',
                meta: {
                    title: '分步引导',
                    permiss: '26',
                },
                component: () => import(/* webpackChunkName: "tour" */ '../views/element/tour.vue'),
            },
            {
                path: '/steps',
                name: 'steps',
                meta: {
                    title: '步骤条',
                    permiss: '27',
                },
                component: () => import(/* webpackChunkName: "steps" */ '../views/element/steps.vue'),
            },
            {
                path: '/form',
                name: 'forms',
                meta: {
                    title: '表单',
                    permiss: '21',
                },
                component: () => import(/* webpackChunkName: "form" */ '../views/element/form.vue'),
            },
            {
                path: '/upload',
                name: 'upload',
                meta: {
                    title: '上传',
                    permiss: '22',
                },
                component: () => import(/* webpackChunkName: "upload" */ '../views/element/upload.vue'),
            },
            {
                path: '/statistic',
                name: 'statistic',
                meta: {
                    title: '统计',
                    permiss: '28',
                },
                component: () => import(/* webpackChunkName: "statistic" */ '../views/element/statistic.vue'),
            },
        ],
    },
    {
        path: '/login',
        meta: {
            title: '登录',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/pages/login.vue'),
    },
    {
        path: '/register',
        meta: {
            title: '注册',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "register" */ '../views/pages/register.vue'),
    },
    {
        path: '/reset-pwd',
        meta: {
            title: '重置密码',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "reset-pwd" */ '../views/pages/reset-pwd.vue'),
    },
    {
        path: '/403',
        meta: {
            title: '没有权限',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/pages/403.vue'),
    },
    {
        path: '/404',
        meta: {
            title: '找不到页面',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "404" */ '../views/pages/404.vue'),
    },
    { path: '/:path(.*)', redirect: '/404' },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    const role = localStorage.getItem('vuems_name');
    const permiss = usePermissStore();
    console.log("permiss",permiss.key);
    // 未登录且当前不是登录页
    if (!role && to.path !== '/login') {
        next('/login');
    }
    // 已登录时访问登录页
    else if (role && to.path === '/login') {
        next(from.path); // 返回原页面
    }
    // 权限验证
    else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
        next('/403');
    }
    else {
        next();
    }
    
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
